package com.seasonsfood.mall.service.dispatch.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.seasonsfood.mall.core.service.impl.BaseServiceImpl;
import com.seasonsfood.mall.service.dispatch.api.domain.DispArea;
import com.seasonsfood.mall.service.dispatch.api.domain.DispSite;
import com.seasonsfood.mall.service.dispatch.api.domain.DispSiteUnit;
import com.seasonsfood.mall.service.dispatch.api.service.DispSiteUnitService;
import com.seasonsfood.mall.service.dispatch.mapper.DispAreaMapper;
import com.seasonsfood.mall.service.dispatch.mapper.DispSiteMapper;
import com.seasonsfood.mall.service.dispatch.mapper.DispSiteUnitMapper;
import com.seasonsfood.mall.util.constant.ResponseCode;
import com.seasonsfood.mall.util.model.ListResponse;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;

@Component("dispSiteUnitService")
@Service(interfaceClass = DispSiteUnitService.class)
public class DispSiteUnitServiceImpl extends BaseServiceImpl<DispSiteUnit> implements DispSiteUnitService {

    @Resource
    DispSiteUnitMapper dispSiteUnitMapper;
    @Resource
    DispSiteMapper dispSiteMapper;
    @Resource
    DispAreaMapper dispAreaMapper;

    @Override
    public ListResponse<DispSiteUnit> siteUnitList(Long parentId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(DispSiteUnit.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentSiteId", parentId);
        criteria.andEqualTo("isDel", 0);
        PageInfo<DispSiteUnit> pageInfo = new PageInfo<>(dispSiteUnitMapper.selectByExample(example));
        return new ListResponse(pageInfo);

    }

    @Override
    public ResponseCode addSiteUnit(DispSiteUnit dispSiteUnit) {
        dispSiteUnit.setCreateTime(new Date());
        int count = dispSiteUnitMapper.insertSelective(dispSiteUnit);
        if (count <= 0) {
            return ResponseCode.ADD_ERROR;
        }
        return ResponseCode.SUCCESS;
    }

    @Override
    public ResponseCode updateSiteUnit(DispSiteUnit dispSiteUnit) {
        System.out.print(dispSiteUnit.toString());
        DispSiteUnit dormerDispSiteUnit = dispSiteUnitMapper.selectByPrimaryKey(dispSiteUnit.getId());

        if (dormerDispSiteUnit.getParentSiteId() != dispSiteUnit.getParentSiteId()&&dispSiteUnit.getParentSiteId()!=null) {
            //原来区域的需要减少用户注册数和购买人数
            DispSite dispSite1 = dispSiteMapper.selectByPrimaryKey(dormerDispSiteUnit.getParentSiteId());
            dispSite1.setRegisterUserNum(dispSite1.getRegisterUserNum() - dormerDispSiteUnit.getRegisterUserNum());
            dispSite1.setBuyUserNum(dispSite1.getBuyUserNum() - dormerDispSiteUnit.getBuyUserNum());
            //更改区域需要增加用户注册数和购买人数
            DispSite dispSite2 = dispSiteMapper.selectByPrimaryKey(dispSiteUnit.getParentSiteId());
            dispSite2.setRegisterUserNum(dispSite2.getRegisterUserNum() + dormerDispSiteUnit.getRegisterUserNum());
            dispSite2.setBuyUserNum(dispSite2.getBuyUserNum() + dormerDispSiteUnit.getBuyUserNum());
            dispSiteMapper.updateByPrimaryKey(dispSite1);
            dispSiteMapper.updateByPrimaryKey(dispSite2);
            DispArea dispArea1 = dispAreaMapper.selectByPrimaryKey(dispSite1.getParentStreetId());
            DispArea dispArea3 = dispAreaMapper.selectByPrimaryKey(dispSite2.getParentStreetId());
            //如果分配的地点位于不同街道增加或减少各自上级区域注册人数
            if (dispArea1.getId() != dispArea3.getId()) {
                dispArea1.setCurrentAreaRegisterUserNum(dispArea1.getCurrentAreaRegisterUserNum() - dormerDispSiteUnit.getRegisterUserNum());
                dispAreaMapper.updateByPrimaryKey(dispArea1);
            /*    String[] temp1;
                String delimeter1 = "/";  // 指定分割字符
                temp1 = dispArea1.getParentIds().split(delimeter1); // 分割字符串
                for (int i = 0; i < temp1.length; i++) {
                    DispArea dispArea2 = dispAreaMapper.selectByPrimaryKey(Long.valueOf(temp1[i]));
                    dispArea2.setCurrentAreaRegisterUserNum(dispArea2.getCurrentAreaRegisterUserNum() - dormerDispSiteUnit.getRegisterUserNum());
                    dispAreaMapper.updateByPrimaryKey(dispArea2);
                }*/
                dispArea3.setCurrentAreaRegisterUserNum(dispArea3.getCurrentAreaRegisterUserNum() + dormerDispSiteUnit.getRegisterUserNum());
                dispAreaMapper.updateByPrimaryKey(dispArea3);
               /* temp1 = dispArea3.getParentIds().split(delimeter1); // 分割字符串
                for (int i = 0; i < temp1.length; i++) {
                    DispArea dispArea4 = dispAreaMapper.selectByPrimaryKey(Long.valueOf(temp1[i]));
                    dispArea4.setCurrentAreaRegisterUserNum(dispArea4.getCurrentAreaRegisterUserNum() + dormerDispSiteUnit.getRegisterUserNum());
                    dispAreaMapper.updateByPrimaryKey(dispArea4);
                }*/
            }

        }

        int count = dispSiteUnitMapper.updateByPrimaryKeySelective(dispSiteUnit);
        if (count <= 0) {
            return ResponseCode.MODIFY_ERROR;
        }
        return ResponseCode.SUCCESS;
    }
}
